package com.bj.disc.base

import androidx.lifecycle.MutableLiveData
import com.ai3d.rfid.base.app.BaseApp
import com.ai3d.rfid.common.log.Logcat
import com.bj.ski.base.database.AppDatabaseSingleton
import com.bj.ski.base.common.utils.LimitingFrequencyUtils
import com.google.gson.Gson
import com.tencent.bugly.crashreport.CrashReport
import com.ym521.logdog.LogDog
import com.ym521.logdog.core.IJsonEngine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/3 17:00
 * @Description : application
 */
class App : BaseApp() {

    companion object {
        lateinit var instance: App
        lateinit var appViewModel: AppViewModel
        val initLiveData = MutableLiveData<String>()
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        AppDatabaseSingleton.init(this)
        initMainThread()
        initCPUThread()
        initIOTread()
        val gson = Gson()

        val logDog = LogDog.Builder()
            .logWriteLogFileEnable(true) //是否开启写入日志文件
            .logFileShowEnable(true)
            .logShowMethodEnable(true) //是否开启显示方法栈信息 默认打印两个方法信息
            .logShowThreadInfoEnable(true) //是否打印当前线程信息
            .build()
        //开发人员自行实现对象转json字符串
        logDog.install(this, object : IJsonEngine {
            override fun <T> toJSON(obj: T): String {
                return gson.toJson(obj)
            }
        })

    }

    private fun initMainThread() {
        InitMgr.initMMKV(this@App)
        initViewModel()
    }

    private fun initCPUThread() {
        GlobalScope.launch(Dispatchers.Default) {
            InitMgr.initMultiDex(this@App)
            InitMgr.initLoadSir()
            //InitMgr.initCaoConfig()
            CrashReport.initCrashReport(getApplicationContext(), "e43a502f2b", true);
        }
    }

    private fun initIOTread() {
        Logcat.d("initIOTread 1111")
        GlobalScope.launch(Dispatchers.IO) {
            InitMgr.initSetting(this@App)
            InitMgr.register(this@App)
        }
    }

    private fun initViewModel() {
        appViewModel = getAppViewModelProvider().get(AppViewModel::class.java)
//        appViewModelInstance.userInfo.observeForever { GlideUtils.loadIconCache(it?.avatar) }
        appViewModel.loginExpired.observeForever {
            if (it) {//弹出token过期dialog
                try {
                    if (LimitingFrequencyUtils.moreThanLastKeyTime(1000)) {
                        com.bj.disc.dialog.HintDialog(
                            com.bj.disc.dialog.HintDialog.PAST_DUE_HINT,
                            InitLanguage.activityList[InitLanguage.activityList.size - 1]
                        ).show()
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }

    }

    override fun onTerminate() {
        super.onTerminate()
        InitMgr.unregisterReceiver(this@App)
    }
}